package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import model.Drug;
import model.Patient;

import service.Service;

public class CreatePrescriptionDialog extends JDialog {
	public boolean modalResult;
	private JButton cancel;
	private JButton create;
	private JLabel lblPatient;
	private JLabel lblDrug;
	private JLabel lblStartDate;
	private JLabel lblEndDate;
	private JLabel lblCountOfUnits;
	private JTextField txtCountOfUnits;
	private JLabel lblRecommendedDose;
	private JTextField txtStartDate;
	private JTextField txtEndDate;
	private JTextField txtMorning;
	private JTextField txtNoon;
	private JTextField txtEvening;
	private JTextField txtNight;
	private JLabel errorLabel;

	private Patient currentPatient = null;
	private Drug currentDrug = null;
	private PrescriptionType currentType;

	private JPanel panelPN;
	private JPanel panelRegular;
	private JPanel panelIrregular;
	private JList listDoses;

	private DefaultListModel listModel;

	private JScrollPane scrollDoses;
	private JButton butCreateDose;
	private JLabel lblHour;
	private JLabel lblCount;
	private JTextField txtHour;
	private JTextField txtCount;

	private Controller controller = new Controller();

	public CreatePrescriptionDialog(Patient patient, Drug drug, PrescriptionType type) {
		currentPatient = patient;
		currentDrug = drug;
		currentType = type;

		// the frame's own attributes...
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setModal(true);
		this.setLayout(null);
		if (currentType.equals(PrescriptionType.PN))
			this.setTitle("PN prescription");
		else if (currentType.equals(PrescriptionType.REGULAR))
			this.setTitle("Daily Regular prescription");
		else
			this.setTitle("Daily Irregular prescription");
		this.setLocation(150, 150);
		this.setSize(360, 360);

		// controls...
		create = new JButton();
		create.setLocation(190, 290);
		create.setSize(140, 25);
		create.setText("Create prescription");
		create.addActionListener(controller);
		this.add(create);

		cancel = new JButton("Cancel");
		cancel.setLocation(40, 290);
		cancel.setSize(120, 25);
		cancel.addActionListener(controller);
		this.add(cancel);

		String p = "Patient: " + currentPatient.getCprno() + " " + currentPatient.getName() + " "
				+ currentPatient.getWeight() + " kg.";
		lblPatient = new JLabel(p);
		this.add(lblPatient);
		lblPatient.setLocation(10, 40);
		lblPatient.setSize(300, 25);

		lblDrug = new JLabel("Drug: " + currentDrug.toString() + "  (" + currentDrug.getUnit()
				+ ")");
		this.add(lblDrug);
		lblDrug.setLocation(10, 70);
		lblDrug.setSize(300, 25);

		lblStartDate = new JLabel("Start date for prescription dd-mm-yyyy");
		this.add(lblStartDate);
		lblStartDate.setLocation(10, 100);
		lblStartDate.setSize(220, 25);

		txtStartDate = new JTextField();
		this.add(txtStartDate);
		txtStartDate.setLocation(240, 100);
		txtStartDate.setSize(90, 25);

		lblEndDate = new JLabel("End date for prescription dd-mm-yyyy");
		this.add(lblEndDate);
		lblEndDate.setLocation(10, 130);
		lblEndDate.setSize(220, 25);

		txtEndDate = new JTextField();
		this.add(txtEndDate);
		txtEndDate.setLocation(240, 130);
		txtEndDate.setSize(90, 25);

		lblRecommendedDose = new JLabel("Recom. count of units per day "
				+ Service.getInstance().recommendedDosePrDay(patient, drug) + " "
				+ currentDrug.getUnit());
		this.add(lblRecommendedDose);
		lblRecommendedDose.setLocation(10, 160);
		lblRecommendedDose.setSize(240, 25);

		panelPN = new JPanel();
		panelPN.setLayout(null);
		this.add(panelPN);
		panelPN.setLocation(10, 190);
		panelPN.setSize(330, 90);
		if (!currentType.equals(PrescriptionType.PN))
			panelPN.setVisible(false);

		lblCountOfUnits = new JLabel("Count of units (" + currentDrug.getUnit() + ")");
		panelPN.add(lblCountOfUnits);
		lblCountOfUnits.setLocation(0, 0);
		lblCountOfUnits.setSize(220, 25);

		txtCountOfUnits = new JTextField();
		panelPN.add(txtCountOfUnits);
		txtCountOfUnits.setLocation(230, 0);
		txtCountOfUnits.setSize(90, 25);

		panelRegular = new JPanel();
		panelRegular.setLayout(new GridLayout(2, 4, 1, 1));
		panelRegular.add(new JLabel("   Morning"));
		panelRegular.add(new JLabel("       Noon"));
		panelRegular.add(new JLabel("   Evening"));
		panelRegular.add(new JLabel("      Night"));
		txtMorning = new JTextField();
		txtNoon = new JTextField();
		txtEvening = new JTextField();
		txtNight = new JTextField();
		panelRegular.add(txtMorning);
		panelRegular.add(txtNoon);
		panelRegular.add(txtEvening);
		panelRegular.add(txtNight);

		this.add(panelRegular);
		panelRegular.setLocation(10, 190);
		panelRegular.setSize(330, 90);
		if (!currentType.equals(PrescriptionType.REGULAR))
			panelRegular.setVisible(false);

		panelIrregular = new JPanel();
		panelIrregular.setLayout(null);
		this.add(panelIrregular);
		panelIrregular.setLocation(10, 190);
		panelIrregular.setSize(330, 90);
		if (!currentType.equals(PrescriptionType.IRREGULAR))
			panelIrregular.setVisible(false);

		butCreateDose = new JButton("Create dose");
		butCreateDose.setLocation(0, 65);
		butCreateDose.setSize(120, 25);
		panelIrregular.add(butCreateDose);
		butCreateDose.addActionListener(controller);

		listModel = new DefaultListModel();
		listDoses = new JList(listModel);
		scrollDoses = new JScrollPane(listDoses);
		scrollDoses.setLocation(180, 0);
		scrollDoses.setSize(140, 90);
		panelIrregular.add(scrollDoses);

		lblHour = new JLabel("TT:MM");
		lblHour.setSize(60, 25);
		lblHour.setLocation(10, 10);
		panelIrregular.add(lblHour);

		lblCount = new JLabel("Amount");
		lblCount.setSize(60, 25);
		lblCount.setLocation(70, 10);
		panelIrregular.add(lblCount);

		txtHour = new JTextField();
		txtHour.setSize(60, 25);
		txtHour.setLocation(0, 30);
		panelIrregular.add(txtHour);

		txtCount = new JTextField();
		txtCount.setSize(60, 25);
		txtCount.setLocation(65, 30);
		panelIrregular.add(txtCount);

		errorLabel = new JLabel("");
		errorLabel.setSize(250, 22);
		errorLabel.setLocation(10, 10);
		this.add(errorLabel);
		errorLabel.setForeground(Color.RED);
	}

	private class Controller implements ActionListener {
		private Service service = Service.getInstance();

		public void actionPerformed(ActionEvent e) {
			errorLabel.setText("");
			try {
				if (e.getSource() == create) {
					GregorianCalendar stDato = Validation.makeDate(txtStartDate.getText());
					GregorianCalendar slDato = Validation.makeDate(txtEndDate.getText());
					if (currentType.equals(PrescriptionType.PN)) {
						if (txtCountOfUnits.getText().length() == 0)
							errorLabel.setText("Count of units");
						else {
							service.createPNPrescription(stDato, slDato, currentPatient,
									currentDrug, Double.parseDouble(txtCountOfUnits.getText()));
							modalResult = true;
							CreatePrescriptionDialog.this.setVisible(false);
						}
					} else if (currentType.equals(PrescriptionType.REGULAR)) {
						double[] doser = { -1, -1, -1, -1 };
						if (!(txtMorning.getText().length() == 0))
							doser[0] = Double.parseDouble(txtMorning.getText());
						if (!(txtNoon.getText().length() == 0))
							doser[1] = Double.parseDouble(txtNoon.getText());
						if (!(txtEvening.getText().length() == 0))
							doser[2] = Double.parseDouble(txtEvening.getText());
						if (!(txtNight.getText().length() == 0))
							doser[3] = Double.parseDouble(txtNight.getText());

						service.createDailyRegularPrescription(stDato, slDato, currentPatient,
								currentDrug, doser[0], doser[1], doser[2], doser[3]);
						modalResult = true;
						CreatePrescriptionDialog.this.setVisible(false);
					} else {
						service.createDailyIrregularPrescription(stDato, slDato, currentPatient,
								currentDrug, Validation.makeHours(listModel.toArray()), Validation
										.makeCounts(listModel.toArray()));
						modalResult = true;
						CreatePrescriptionDialog.this.setVisible(false);
					}

				} else if (e.getSource() == cancel) {
					CreatePrescriptionDialog.this.setVisible(false);
					modalResult = false;
				} else if (e.getSource() == butCreateDose) {
					if (txtHour.getText().length() != 5)
						errorLabel.setText("Correct hour");
					else if (txtCount.getText().length() == 0)
						errorLabel.setText("Count of units");
					else {
						listModel.addElement(txtHour.getText() + " " + txtCount.getText());
						txtHour.setText("");
						txtCount.setText("");
					}
				}
			} catch (RuntimeException ex) {
				errorLabel.setText(ex.getMessage());
			}
		}
	}
}
